package com.zzsy.carosshopprojects.base;

import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;

import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.ViewModel;

/**
 * 基类VM
 */
public class BaseViewMode extends ViewModel implements DefaultLifecycleObserver {
    public getTokensListener getTokensListener;

    /**
     * 获取token
     * @param context
     * @param getTokensListener
     */
    public void getToken(Context context,getTokensListener getTokensListener) {
        RequestUtils.getToken(context, new MyObserver<BaseBean>(context,true) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String tokenMsg, int code) {
                if (code == ConfigureFile.code) {
                    getTokensListener.getTokens(tokenMsg);
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), tokenMsg);
                }

            }
        });
    }

    public void setGetTokensListener(getTokensListener getTokensListener) {
        this.getTokensListener = getTokensListener;
    }

    public interface getTokensListener {
        void getTokens(String token);
    }

}
